package com.woniuxy.creditsystem.systemmag.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.creditsystem.common.bean.PageRequest;
import com.woniuxy.creditsystem.common.bean.ProcessModelInfo;
import com.woniuxy.creditsystem.common.mapper.ProcessModelMapper;
import com.woniuxy.creditsystem.systemmag.service.IProcessModelService;
import com.woniuxy.creditsystem.systemmag.util.ImportExcel;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;

@Service
@Transactional(readOnly = true)
public class ProcessModelServiceImpl implements IProcessModelService {

    @Resource
    private ProcessModelMapper processModelMapper;
    @Override
    public PageInfo<ProcessModelInfo> findAllByParams(PageRequest request, ProcessModelInfo pro) {
        return PageHelper.startPage(request.getPageNum(),request.getLimit()).
                doSelectPageInfo(() -> processModelMapper.findAllByParams(request,pro));
    }

    /**
     * 新增数据
     * @Author liaojian
     * */
    @Transactional(isolation = Isolation.REPEATABLE_READ,rollbackFor = Exception.class)
    @Override
    public int save(ProcessModelInfo pro) {
        return processModelMapper.save(pro);
    }

    /**
     * 修改数据
     * */
    @Transactional(isolation = Isolation.REPEATABLE_READ,rollbackFor = Exception.class)
    @Override
    public int update(ProcessModelInfo pro) {
        return processModelMapper.update(pro);
    }

    @Transactional(isolation = Isolation.REPEATABLE_READ,rollbackFor = Exception.class)
    @Override
    public int delete(long id) {
        return processModelMapper.delete(id);
    }

    /**
     * 导入数据
     * @Author liaojian
     * */
    @Transactional(isolation = Isolation.REPEATABLE_READ,rollbackFor = Exception.class)
    @Override
    public int saveList(MultipartFile file) throws IOException {
        List<ProcessModelInfo> lists = null;
        try {
            lists = ImportExcel.getProList(file);
        } catch (Exception e) {
            e.printStackTrace();
        }
        for(ProcessModelInfo pro : lists){
            processModelMapper.save(pro);
        }
        return lists.size();
    }
}
